﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MotionControl;
using System.Threading;
using System.IO.Ports;
using System.Collections.Concurrent;
using HelperTool_WHW;
using UserControlHelper;
using HalconDotNet;
using System.Drawing;
using System.Windows.Forms;
using XXTUserUI;
using RFIDDLL;
using System.Diagnostics;

namespace MainFrame
{
    public delegate int buttonControl(string buttonText);
    public delegate void m_imageDisplay(HObject obj);
    public delegate void m_imageDisplay_obj(HObject obj,string color);
    public delegate int _ChangeModel(string ModelName);
    public delegate void m_homeTips(string tips);
    public delegate void Pause();
    public delegate void _DrawRoiRegion();
    public delegate void _displayClycleTime();
    public delegate void _displayResultImage();
    public delegate void m_cam_softTrigger(int camIndex, e_cam_procType proT);
    public class GlobalVar
    {
        public static e_WorkMode curWorkMode = e_WorkMode.生产模式;
        public static bool IsMesTCPOK = false;
        public static Stopwatch sw_CT = new Stopwatch();
        public static Stopwatch sw_CT2 = new Stopwatch();
        public static RFIDbox RFIDBox_in;
        public static RFIDbox RFIDBox_out;
        public static _displayResultImage DisplayResult_cur;
        public static _displayResultImage imageDisplayResult_cur;
        public static _displayResultImage imageDisplayResult_Last;
        public static bool IsUserLogOk = false;
        public static buttonControl btn_Home;
        public static buttonControl btn_Start;
        public static buttonControl btn_Estop;
        public static _ChangeModel ChangeModel;
        public static _ChangeModel RefreshModel;
        public static bool IsTip = false;
        public static bool IsTipOK = false;
        public static bool IsTipNG = false;
        public static bool IsTip_fly = false;
        public static bool IsTipOK_fly = false;
        public static bool IsTipNG_fly = false;
        /// <summary>
        /// NG输送运行一段距离
        /// </summary>
        public static bool IsStartConveryNG = false;
        /// <summary>
        /// 自己放行，不能要料信号
        /// </summary>
        public static bool IsRealsePro = false;
        public static bool IsAutoAddData = false;
        public static bool IsOldTest = false;
        public static bool IsFirstLoadConfig = true;
        public static BoxPort Box1;
        public static BoxPort Box2;
        public static DateTime StartTime_Cut = DateTime.Now;
        public static DateTime StartTime_Label = DateTime.Now;
        public static _displayClycleTime displayCycleTime_Cut;
        public static _displayClycleTime displayCycleTime_Label;
     
        public static _displayClycleTime displayCycleTime_CutReal;
        public static _displayClycleTime displayCycleTime_LabelReal;
        public static _displayClycleTime displayCycleTime_label_STOP;

        public static _DrawRoiRegion Image_drawRoiRegion1;
        public static _DrawRoiRegion Image_drawRoiRegion2;
        public static _DrawRoiRegion Image_drawRoiRegion3;
        public static _DrawRoiRegion Image_clearRoiRegion1;
        public static _DrawRoiRegion Image_clearRoiRegion2;
        public static _DrawRoiRegion Image_clearRoiRegion3;

        /// <summary>
        /// 雕刻完成
        /// </summary>
        public static bool IsLaserMarking = false;
        /// <summary>
        /// 雕刻完后开始扫码
        /// </summary>
        public static bool IsLaserScaning = false;
        /// <summary>
        /// 工装已到雕刻Z坐标
        /// </summary>
        public static bool IsLaserArrial = false;
        public static bool IsLaserReadly1 = false;
        public static bool IsLaserReadly2 = false;


        public static bool IsStep = false;
        public static bool isChangeUser = false;
        public static MainFormWG frmMainNew = null;
        public static frm_MainFrame frmMain = null;
        public static bool is_regedit = false;
        public static ImageReult curResult_Pos = new ImageReult();
        public static ImageReult lastResult_Pos = new ImageReult();
        /// <summary>
        /// 输送带上当前产品个数
        /// </summary>
        public static int num_Convery=0;
        public static m_imageDisplay Imagedispay_curMain;//图像显示委托
        public static m_imageDisplay Imagedispay_curMain_obj;//图像显示委托
        public static m_imageDisplay Imagedispay_lastMain;//图像显示委托
        public static m_imageDisplay Imagedispay_lastMain_obj;//图像显示委托

        public static m_imageDisplay imageDisplay1;//图像显示委托
        public static m_imageDisplay_obj imageDisplay1_obj;//图像显示委托
        public static m_imageDisplay imageDisplay2;//图像显示委托
        public static m_imageDisplay_obj imageDisplay2_obj;//图像显示委托
        public static m_imageDisplay imageDisplay3;//图像显示委托
        public static m_imageDisplay_obj imageDisplay3_obj;//图像显示委托
        public static m_homeTips homeTips;//回原提示委托
        public static m_cam_softTrigger camSoftTrigger;//相机触发委托
        public static string TypeName_New = "";            //要保存的型号规格名称
        public static bool isCloseSaveAS = false;          //输入型号名称的窗口是否已关闭
        public static string type_path = Application.StartupPath + "\\typeFile";  //型号规格数据存放路径
        public static string type_copy_path = Application.StartupPath + "\\typeFileCopy";  //型号规格数据存放路径
        public static CloseSave close_save = new CloseSave();             //软件关闭时，保存的数据，主要是当前配方
        public static string currentSelectTypeName = "";   //当前选择的型号规格名称
        public static string closeSavePara_path = Application.StartupPath + "\\closeSavePara.csp";//关软件保存数据


     
        public static HWindow hwindow_cam_1_hwin;//用于图像显示
        public static HWindow hwindow_cam_2_hwin;//用于图像显示
        public static HObject Cam_1_hobj = null;//用于图像显示
        public static HObject Cam_2_hobj = null;//用于图像显示
        public static HTuple hv_calibrate_real_x = new HTuple();//标定时实际坐标x数组
        public static HTuple hv_calibrate_real_y = new HTuple();//标定时实际坐标y数组
        public static HTuple hv_calibrate_pix_x = new HTuple();//标定时实际坐标x数组
        public static HTuple hv_calibrate_pix_y = new HTuple();//标定时实际坐标y数组
        public static e_cam_procType imageProcMode_1 = e_cam_procType.justDisplay;//图像处理类型(只显示、标定、正常处理)
        public static e_cam_procType imageProcMode_2 = e_cam_procType.justDisplay;//图像处理类型(只显示、标定、正常处理)
        public static string calibrationPath = Application.StartupPath+ "\\Calibration";//标定文件路径

        public static HTuple hv_HomMat2DRotate90 = new HTuple();
        public static double[] cam1_result = new double[6];//相机1的图像处理结果
        public static double[] cam2_result = new double[6];//相机2的图像处理结果
        /// <summary>
        /// 急停后需要回零
        /// </summary>
        public static bool isAllHome = false;//所有轴回原

        public static FormHomeTips frmHomeTips =new FormHomeTips();
        /// <summary>
        /// 软件配置
        /// </summary>
        public static ConfigData userConfig = new ConfigData();
        /// <summary>
        /// 配方文件
        /// </summary>
        public static MotionConfig motionConfig = new MotionConfig();
        public static char char_VersionSpilt ='~';
        /// <summary>
        /// 关软件时保存
        /// </summary>
        public static closeStartConifg closeSave = new closeStartConifg();
        public static string configPath = Environment.CurrentDirectory+"\\Config\\configPara.cpa";
        /// <summary>
        /// 飞达开始出标一张
        /// </summary>
        public static bool IsflyStart1 = false;
        /// <summary>
        /// 飞达开始出标一张
        /// </summary>
        public static bool IsflyStart2 = false;
        public static string motionConfigPath = Environment.CurrentDirectory + "\\Config\\motionConfig.mcg";
        public static string closeSavePath = Environment.CurrentDirectory + "\\Config\\closeSave.cse";
        public static bool isFlyingWithoutWord1= false;
        public static bool isFlyingWithoutWord2= false;
        public static bool isFlying1OK = false;
        public static bool isFlying2OK = false;
        public static bool IsManThrought = false;
        public static bool IsThrought = false;
        public static bool IsStatusAuto = false;
        public static bool IsTip_Start = false;
        public static bool IsTip_down = false;
        public static bool IsStart_EndWork = false;
        #region 数据库相关
        //数据库相关
        public static string dbPassword = "";//权限管理数据库操作密码
        public static string dbName = "User.db";//权限管理数据库名称
        public static DB_base dbOperate = new sqliteHelper(dbName, dbPassword);//初始化权限管理数据库
        public static string tableName = "UserInfo";//权限管理表名称
        public static string[] tableCols = new string[] { "UserName", "Password", "Level" };//权限管理列名称数组
        public static string[] tableTypes = new string[] { "TEXT", "TEXT", "TEXT" };//列名称类型数组

        #endregion
        #region 界面更新相关
        public static volatile bool isAutoRun = false;//是否在工作状态
        /// <summary>
        /// 除尘
        /// </summary>
        public static bool IsAirCut = false;
        public static volatile bool isEstop = false;//急停
        public static volatile bool isPause = false;//暂停操作
        public static volatile bool isHome = false;//回原操作
        public static volatile bool isHardStart = false;
        public static volatile bool isCanMotion = !GlobalVar.isEstop && !GlobalVar.isHome && !GlobalVar.isPause && !GlobalVar.isExit;
        #endregion
        /// <summary>
        /// 进料段
        /// </summary>
        public static List<ProductInfo> front_pros = new List<ProductInfo>();
        /// <summary>
        /// 镭雕段
        /// </summary>
        public static List<ProductInfo> Laser_pros = new List<ProductInfo>();
        /// <summary>
        /// 贴膜段
        /// </summary>
        public static List<ProductInfo> Label_pros = new List<ProductInfo>();
        //提示窗口对应成品变量
        public static volatile warningErrorInfo[] warningTips = new warningErrorInfo[8];//警告弹窗对应的5个成员 0-4 对应warningType        
        public static volatile warningErrorInfo[] errorTips = new warningErrorInfo[8];//错误弹窗对应的8个成员，0-7对应errorType
        public static bool isExit = false;//退出程序或退出自动运行
        public static bool isReseting = false;//复位
        public static string CSVpath;//数据保存路径
        public static volatile platePosConvert plateConvert1 = new platePosConvert();//平台1位置转换信息
        public static volatile platePosConvert plateConvert2 = new platePosConvert();//平台1位置转换信息
        public static List<HikVision> cameraList = new List<HikVision>();
        public static bool Cut_Start = false;
        public static bool Cut_Finish = true;//放行治具
        public static bool IsManFinishSignal = false;
        /// <summary>
        /// 人工完成信号
        /// </summary>
        public static bool isManFinish = false;
        /// <summary>
        /// 开始人工信号
        /// </summary>
        public static bool isMan_Start = false;
        /// <summary>
        /// 开始贴标信号
        /// </summary>
        public static bool Label_Start = false;
        /// <summary>
        /// 贴标完成
        /// </summary>
        public static bool LabelFinshed = true;
        /// <summary>
        /// 扫码完成
        /// </summary>
        public static bool ScanFinished = false;
        public static bool IsProccessed1 = false;
        public static bool IsProccessed2 = false;
        public static bool IsTwoPos = false;
        /// <summary>
        /// 下相机定位
        /// </summary>
        public static e_Camera_Pos e_Camera_Pos1 = e_Camera_Pos.未拍照;
        public static e_Camera_Pos e_Camera_Pos2 = e_Camera_Pos.未拍照;
      
        public static e_Camera_Pos e_Camera_Word1 = e_Camera_Pos.未拍照;
        public static e_Camera_Pos e_Camera_Word2 = e_Camera_Pos.未拍照;

        public static bool IsCheckTwo = false;
        public static e_Camera_Pos e_Camera_Check1 = e_Camera_Pos.未拍照;
        public static e_Camera_Pos e_Camera_Check2 = e_Camera_Pos.未拍照;
        /// <summary>
        /// 左侧产品另外半边是否需要检测
        /// </summary>
        public static bool HalfDut1NeedCheck = false;
        /// <summary>
        /// 右侧产品另外半边是否需要检测
        /// </summary>
        public static bool HalfDut2NeedCheck = false;

        public static bool IsLabelPosTwo = false;
        /// <summary>
        /// 治具定位
        /// </summary>
        public static e_Camera_Pos e_Camera_LabelPos1 = e_Camera_Pos.未拍照;
        public static e_Camera_Pos e_Camera_LabelPos2 = e_Camera_Pos.未拍照;

        public static bool IsCutTwo = false;
        public static e_Camera_Pos e_Camera_Cut1 = e_Camera_Pos.未拍照;
        public static e_Camera_Pos e_Camera_Cut2 = e_Camera_Pos.未拍照;
        /// <summary>
        ///结束作业
        /// </summary>
        public static bool IsEndWork = false;
    
        public static sqlHelperDapper sqlHelper = null;
        public static bool IsMaunalDown=false;
        public static bool air1up_start = false;
        public static bool air2up_start = false;
        public static bool air12up_start = false;
        public static bool air1up_end = false;
        public static bool air2up_end = false;
        public static bool air12up_end = false;
        public static bool air1down_start = false;
        public static bool air2down_start = false;
        public static bool air12down_start = false;
        public static bool air1down_end = false;
        public static bool air2down_end = false;
        public static bool air12down_end = false;
        public static bool IsPosBeforeLabel = false;
        public static MesTCP mesTcp;
        public static List<typeVersion> tv_list = new List<typeVersion>();
        public static e_Status curStatus = e_Status.未知;
        /// <summary>
        /// Mes触发的急停  在复位时会自动取消急停
        /// </summary>
        public static bool IsMesEstop = false;
        public static bool IsTcpReset = false;
        public static frm_Main frmAxisConfig;

        public static TcpIP VisionTcpIP = new TcpIP();//海康视觉通信接口
        public static port_Class LightSourceController = new port_Class();//光源控制器接口

    }
    public class typeVersion
    {
        public string type { get; set; }
        public string Version { get; set; }
        public string GetTypeVersion()
        {
            if (string.IsNullOrEmpty(type) || string.IsNullOrEmpty(Version))
                return null;
            return type + GlobalVar.char_VersionSpilt + Version;
        }
        public string GetConfig()
        {
            if (string.IsNullOrEmpty(Version)|| !Version.Contains(" ")|| !(Version.Split(' ').Length>1))
                return null;
            return Version.Split(' ')[1];
        }
    }
   public enum e_Camera_Pos
    {
        未拍照,
        已拍照OK,
        已拍照NG,
    }

   
    public enum e_LightState
    {
        报警,
        全灭,
        运行中,
        暂停,
        提示,
        None
    }

    /// <summary>
    /// 警告弹窗对应的类型
    /// </summary>
    public enum e_warningType
    {
        addMaterial,//加料警告
        removeMaterail,//贴标
        sucker,//吸气警告
        motor,//电机
        isFlip//视觉
    }
    /// <summary>
    /// 错误弹窗对应的类型
    /// </summary>
    public enum e_errorType
    {
        axisError = 0,//轴报警
        weldError,
        cylinderError,//贴胶报警
        homeError,//回零报警
        imageError,//图像错误
        softEstop//软件急停
    }

    public enum e_cam_procType
    {
        /// <summary>
        /// 显示
        /// </summary>
        justDisplay=0,
        /// <summary>
        /// 标定
        /// </summary>
        calibrate=1,
        /// <summary>
        /// 处理
        /// </summary>
        identify = 2,
    }
    public class platePosConvert
    {
        /// <summary>
        /// 像素矩阵
        /// </summary>
        public HTuple hMat2d_Pix = new HTuple();
        /// <summary>
        /// 原像素坐标C
        /// </summary>
        public double drow { get; set; }
        public double dcol { get; set; }
        /// <summary>
        /// 物理矩阵
        /// </summary>
        public HTuple hMat2d_Act = new HTuple();

    }
}
